package session.stateless.CRM;

import entity.CRM.Customer;
import entity.CRM.LineItem;
import entity.CRM.SaleOrder;
import entity.CRM.SaleLead;
import entity.CRM.SaleQuotation;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import session.stateless.FRM.CreditAccountBeanLocal;

@Stateless
public class SaleOrderBean implements SaleOrderBeanLocal {
    @PersistenceContext()
    EntityManager em;
    SaleOrder saleOrder;
    @EJB
    CustomerBeanLocal customerSessionBeanLocal;
    @EJB
    SaleLeadBeanLocal saleLeadBeanLocal;
    @EJB
    DiscountBeanLocal discountBeanLocal;
    @EJB
    CreditAccountBeanLocal creditAccountBeanLocal;
    
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    
    public Long convertToSaleOrder(String reg_num, Long saleQuotationId, List<LineItem> lineItems) 
    {
        Customer customer = em.find(Customer.class, reg_num);
        
        if (customer == null)
        {
            SaleLead saleLead = em.find(SaleLead.class, reg_num);
            if (saleLead != null)
            {
                customerSessionBeanLocal.convertCustomerFromSaleLead(saleLead.getRegistrationNumber());
                customer = em.find(Customer.class, reg_num);
            }            
        }
        
        SaleQuotation saleQuotation = em.find(SaleQuotation.class, saleQuotationId);        
        
        saleOrder = new SaleOrder();
        
        saleOrder.setCustomer(customer);
        saleOrder.setSaleQuotation(saleQuotation);
        saleOrder.setLineItems(lineItems);
        Date date = new Date();
        saleOrder.setSaleOrderDate(dateFormat.format(date));
        em.persist(saleOrder);
        
        customer.getSaleOrders().add(saleOrder);
        em.flush();
        
        creditAccountBeanLocal.modifyCreditBalance_AddSaleOrder(customer, saleOrder.getTotalPrice());
        
        return saleOrder.getId();
    }

    public List<SaleOrder> getSaleOrders() {
        Query q = em.createQuery("SELECT c FROM SaleOrder c");
        return q.getResultList();
    }
    
    public List<SaleOrder> getSaleOrdersByCustomer(String reg_num) {
        Customer customer = em.find(Customer.class, reg_num);
        return customer.getSaleOrders();
    }
    
    public void updateSaleOrder(Long saleOrderId, List<LineItem> lineItems){
        SaleOrder saleOrder = em.find(SaleOrder.class, saleOrderId);
        if (saleOrder == null)
            System.out.println("Sale Order does not exist. Please try again.");
        else
        {
            saleOrder.setLineItems(lineItems);
            em.flush();
        }
    }
    
    public void deleteSaleOrder(Long saleOrderId)
    {
        SaleOrder saleOrder = em.find(SaleOrder.class, saleOrderId);
        if(saleOrder == null)
            System.out.println("No Sale Order Found.");
        else
           {
//            Customer temp_customer = saleOrder.getCustomer();
//            List<SaleOrder> temp_saleOrders = temp_customer.getSaleOrders();
//            temp_saleOrders.remove(saleOrder);
//            temp_customer.setSaleOrders(temp_saleOrders);
//          
            List<LineItem> temp=saleOrder.getLineItems();
            saleOrder.setLineItems(null);
            
            for (LineItem lineItem: temp)
            {
                em.remove(lineItem);
            }
            saleOrder.getCustomer().getSaleOrders().remove(saleOrder);
            em.flush();
            em.remove(saleOrder);
            em.flush();
        }
    }

    public SaleOrder findSaleOrder(Long saleOrderId) {
        return (SaleOrder) em.find(SaleOrder.class, saleOrderId);
    }

    public boolean havingCashInAdvance(Long saleOrderId) {
        SaleOrder saleOrder = em.find(SaleOrder.class, saleOrderId);
        if (saleOrder.getCashInAdvance()!=null)
        {
            if (saleOrder.getCashInAdvance().getCashInAdvanceAmount()>0)
                return true;
            else return false;            
        }
        return false;
    }

}
